/**
 * Copyright (c) 2016-2023, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jpress.module.article.controller.admin;

import com.jfinal.aop.Inject;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jpress.JPressConsts;
import io.jpress.commons.flowable.FlowConsts;
import io.jpress.commons.layer.SortKit;
import io.jpress.model.Department;
import io.jpress.module.article.model.Article;
import io.jpress.module.article.service.ArticleCategoryService;
import io.jpress.module.article.service.ArticleService;
import io.jpress.service.DepartmentService;
import io.jpress.service.UserService;
import io.jpress.web.base.AdminControllerBase;
import io.jpress.core.flowable.DelegateInfo;
import io.jpress.core.flowable.FlowableUtil;
import org.flowable.engine.RuntimeService;
import org.flowable.task.api.Task;

import java.util.LinkedList;
import java.util.List;

/**
 * @author Michael Yang 杨福海 （fuhai999@gmail.com）
 * @version V1.0
 * @Package io.jpress.module.article.admin
 */
@RequestMapping(value = "/admin/article/bpmn", viewPath = JPressConsts.DEFAULT_ADMIN_VIEW)
public class _ArticleBnpmController extends AdminControllerBase {

    @Inject
    private ArticleService articleService;

    @Inject
    private ArticleCategoryService categoryService;

    @Inject
    private DepartmentService departmentService;

    @Inject
    private UserService userService;


    /**
     * 由 {@link io.jpress.web.admin._FlowController} 的 templateDesign() 转发过来的
     * 流程设计
     */
    public void design() {
        List<Department> departments = departmentService.findAll();
        SortKit.toLayer(departments);
        setAttr("departments", departments);


        LinkedList<DelegateInfo> serviceTaskDelegates = FlowableUtil.getServiceTaskDelegates("article");
        setAttr("serviceTaskDelegates", serviceTaskDelegates);
        render("article/article_bpmn_design.html");
    }


    /**
     * 由 {@link io.jpress.web.admin._FlowController} 的 todoDetail() 转发过来的
     * 待办任务内容详情，使用 layer 弹出
     */
    public void detail() {
        Task task = getAttr("task");

        RuntimeService runtimeService = FlowableUtil.getRuntimeService();
        Long articleId = (Long) runtimeService.getVariable(task.getProcessInstanceId(), FlowConsts.VAR_CONTENT_ID);

        Article article = articleService.findById(articleId);
        setAttr("article", article);

        render("article/article_bpmn_detail.html");
    }


    /**
     * 多实例完成条件描述
     */
    public void description(){
        render("article/article_bpmn_description.html");
    }


}
